package com.fingard.dsp.bank.directbank.gzdsf01;

import java.util.Date;

import com.fingard.FGBiz;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.TaskTimerSet;
import com.fingard.dsp.bank.directbank.bankInterface.IScheduleTask;
import com.fingard.text.StringHelper;
import com.fingard.util.DateHelper;


public class GZDSF01ScheduleTask extends GZDSF01Base implements IScheduleTask {

    public void execTask(TaskTimerSet timerSet) {
        try {
            if (timerSet.taskNameList.toLowerCase().contains("keepalive")) {
            	keepAlive();
            }
            
        } catch (Exception ex) {
            this.WriteBankLogLn(ex);
        }
    }

    public void keepAlive(){
    	String logCode = "GZDSF01ScheduleTask.keepAlive";
		try {
			if(StringHelper.isNullOrEmpty(bankFront.loginFlag)){
				FGBiz.logTrace.info(logCode, "未登录");
				if(bankFront.lastLoginTime == null){
					FGBiz.logTrace.info(logCode, "进行首次登录");
					this.login();
				}
				return;
			}
			if(bankFront.lastVisitTime == null){
				FGBiz.logTrace.info(logCode, "未初始化上次交互时间");
				return;
			}
			int hbInterval = getBankConfig().getValueAsInt("heartbeatInterval");
			if(hbInterval <= 0){
				hbInterval = 180;
			}
			double diffSecs = DateHelper.getDifferSecs(new Date(), bankFront.lastVisitTime);
			if(diffSecs >= hbInterval){
				StringBuilder tmpSb = new StringBuilder();
		    	tmpSb.append("900199");// 交易种类
		    	tmpSb.append("1202");// 报文类型
		    	tmpSb.append(String.format("%-12s", bankFront.org));// 发起方代码（收费机构代码）
				tmpSb.append(String.format("%-12s",bankFront.serverCode));// 接收方代码（代收付中心行号）"906581000018"
				tmpSb.append(String.format("%-8s", ""));// 返回码
				tmpSb.append(Format.DateTime14Format.format(new Date()));//发起日期和时间n14(YYYYMMDDhhmmss)
				tmpSb.append(String.format("%-64s", ""));// 校验码
				
				String[] retStr = encryptAndSend(tmpSb.toString());
				if(StringHelper.hasAnyChar(retStr[0])){
					FGBiz.logTrace.error(logCode, "通讯链路检测执行失败，"+retStr[0]+"-"+retStr[1]);
				}else{
					FGBiz.logTrace.info(logCode, "通讯链路检测发送正常");
				}
			}else{
				FGBiz.logTrace.debug(logCode, "未到间隔时间，暂不执行");
			}
		} catch (Exception ex) {
			FGBiz.logTrace.exception(logCode, ex);
		}
    }
    
}
